package com.doform.android.xianggucheng.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.activity.money.BillActivity;
import com.doform.android.xianggucheng.activity.money.LJSYActivity;
import com.doform.android.xianggucheng.activity.money.QRNHActivity;
import com.doform.android.xianggucheng.activity.money.TiXianActivity;
import com.doform.android.xianggucheng.bean.ALBBBean;
import com.doform.android.xianggucheng.bean.MoneyBean2;
import com.doform.android.xianggucheng.bean.YHKBean;
import com.doform.android.xianggucheng.bean.request_bean.ReEarning;
import com.doform.android.xianggucheng.bean.request_bean.ReqBank2;
import com.doform.android.xianggucheng.bean.request_bean.ReqYHKBean;
import com.doform.android.xianggucheng.retrofit.ApiSpecTransformer.DialogTransformer;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.widget.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者 Y_MS
 * Created by ${APEN} on 2017-07-15.
 * GitHub：https://github.com/cxydxpx
 */

public class MoneyFragment extends BaseFragment {

    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;

    @BindView(R.id.rl_ljsy)
    RelativeLayout rl_ljsy;

    @BindView(R.id.rl_qrnh)
    RelativeLayout rl_qrnh;

    @BindView(R.id.tv_all_money)
    TextView tv_all_money;

    @BindView(R.id.tv_ljsy)
    TextView tv_ljsy;

    @BindView(R.id.titleBar)
    TitleBar titleBar;

    @BindView(R.id.tv_kyye)
    TextView tv_kyye;

    @BindView(R.id.tv_yesterday_sy)
    TextView tv_yesterday_sy;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_money;
    }

    @Override
    protected void init() {
        super.init();
    }

    @Override
    protected void initData() {
        super.initData();
        initEarnData("1");
        initHangqing();
    }

    private void initHangqing() {
//        HttpManager
//                .instance()
//                .getHangQing(new ReqHang("1"))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new ErrorHandleSubscriber<HangQingBean>() {
//                    @Override
//                    public void onNext(@NonNull HangQingBean s) {
//
//                    }
//                });
    }


    /**
     * 获取 银行 图标
     *
     * @param bankcardno 卡号
     * @return
     */
    private Observable<ALBBBean> getYHKTB(String bankcardno) {
        return HttpManager
                .instance()
                .getBankData2(new ReqBank2(bankcardno));

    }


    private void initEarnData(String pageid) {
        HttpManager
                .instance()
                .getYHKData(new ReqYHKBean())
                .subscribeOn(Schedulers.io())
                .flatMap(new Function<YHKBean, Observable<ALBBBean>>() {
                             @Override
                             public Observable<ALBBBean> apply(@NonNull YHKBean yhkBean) throws Exception {
                                 return getYHKTB(yhkBean.getBankcardno());
                             }
                         }
                ).subscribe(new ErrorHandleSubscriber<ALBBBean>() {
                                @Override
                                public void onNext(@NonNull ALBBBean albbBean) {

                                }
                            }
        );

        HttpManager
                .instance()
                .getEarnings(new ReEarning(pageid))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(new DialogTransformer<MoneyBean2>(mProgressBar))
                .subscribe(new ErrorHandleSubscriber<MoneyBean2>() {
                    @Override
                    public void onNext(@NonNull MoneyBean2 moneyBean2) {
                        if (moneyBean2 != null) {
                            mTotalamount = Double.parseDouble(moneyBean2.getTotalamount()) / 100;
                            mTotalprofit = moneyBean2.getTotalprofit();
                            mAvailableamout = Double.parseDouble(moneyBean2.getAvailableamount()) / 100;
                            mDayprofit = moneyBean2.getDayprofit();

                            tv_all_money.setText(String.valueOf(mTotalamount));//账户总额
                            tv_ljsy.setText(mTotalprofit);//累计收益
                            tv_kyye.setText(String.valueOf(mAvailableamout));//可用金额
//                            tv_yesterday_sy.setText(mDayprofit);//昨日收益

                        }
                    }
                });
    }

    private Double mTotalamount;
    private Double mAvailableamout;
    private String mTotalprofit = "0.00";
    private String mDayprofit;

    @Override
    protected void showView() {
        super.showView();
        titleBar.setTextColor(getResources().getColor(R.color.black));
        titleBar.setBackgroundColor(new ColorDrawable(getResources().getColor(R.color.white)));
        titleBar.setTextTitle("我的账户");
        titleBar.setBackGone();
    }

    @OnClick({R.id.rl_bill, R.id.rl_cash, R.id.rl_ljsy, R.id.rl_qrnh})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.rl_ljsy://累计收益
                startActivity(new Intent(mContext, LJSYActivity.class));
                break;
            case R.id.rl_qrnh://七日年化
                startActivity(new Intent(mContext, QRNHActivity.class));
                break;
            case R.id.rl_bill://账单
                startActivity(new Intent(mContext, BillActivity.class));
                break;
            case R.id.rl_cash://提现
                startActivity(new Intent(mContext, TiXianActivity.class));
                break;
            default:
                break;
        }

    }

}
